import "dotenv/config.js";

import { Command } from "commander";
import db from "../src/database.js";

import { makePassword } from "../src/utils.js";

const program = new Command();
program
  .name("ChangePassword")
  .description("cli to change user password by email")
  .version("0.0.1");
program
  .requiredOption("-e, --email <email>", "email")
  .requiredOption("-p, --password <password>", "password")
  .parse(process.argv);

const options = program.opts();

(async () => {
  const user =
    await db.findOne`SELECT * FROM User where email = ${options.email}`;
  if (!user) {
    console.error("user not found");
    return await db.close();
  }
  const password = makePassword(options.password);
  await db.run`UPDATE User SET password = ${password} where id = ${user.id}`;
  await db.close();
})();
